package com.token.iot.bratterycharging.boss.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.transaction.OutstandingTransactionCallback;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.ReturnCode;
import com.outstanding.framework.core.SysCode;
import com.token.batterycharging.account.api.AccountDTO;
import com.token.iot.bratterycharging.boss.dao.AgentDAO;
import com.token.iot.bratterycharging.boss.dao.MerchantDAO;
import com.token.iot.bratterycharging.boss.dao.UserDAO;
import com.token.iot.bratterycharging.boss.web.Agents;
import com.token.iot.bratterycharging.boss.web.Merchant;
import com.token.iot.bratterycharging.boss.web.ResCode;

/**
 * 
 * @author chenzhixuan
 *
 */
@Service
public class MerchantService {

	@Autowired
	private MerchantDAO dao;

	@Autowired
	private AgentDAO adao;

	public String addMerchant(Merchant value, Long operId) throws PendingException {
		if (operId != null) {
			value.setOperId(operId);
		}
		if (!value.vaildateForCreate()) {
			throw new PendingException(ResCode.undenfinued);
		}
		Agents agent = adao.selectOneById(value.getAgentId());
		if (agent == null) {
			throw new PendingException(ResCode.AgentNotFind);
		}
		dao.insertMerchant(value);
		return "success";

	}

	public List<Merchant> listall(String agentId) throws PendingException {
	
		List<Merchant> list = dao.list(agentId);
		
		return list;

	}
	
	public PageInfo list(int pageNo, int pageSize,String agentId) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		// 执行sql
		List<Merchant> list = dao.list(agentId);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}
	
	public PageInfo list(int pageNo, int pageSize,String agentId,String merchantName) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		// 执行sql
		List<Merchant> list = dao.listbyMerchantName(agentId,merchantName);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}
	
	
	public Merchant detail(String merchantid)throws PendingException {
		return dao.selectOneById(merchantid);
	}
	
	public Merchant selectByUserNamePassword(String userName, String password) throws PendingException{
		return dao.selectByUserNamePassword(userName, password);
	}
}
